
#ifndef QG_LOCAL_COMM_ENTRANCE_NETWORK_SERVICE_INCLUDE_CNET_CONFIG_H_
#define QG_LOCAL_COMM_ENTRANCE_NETWORK_SERVICE_INCLUDE_CNET_CONFIG_H_

#include "CcmdBase.h"
#include "cmdHandle/CNetBase.h"

/**************************************************************************
Date: 2023-12-18
Description: CMD
配置管理
**************************************************************************/

NETWORKSERVICE_BEGIN_NAMESPACE

#define MQTT_DEFAULT_TEXT "__DEFAULT__"
#define DEFALUT_URL_HOST  "http://192.168.0.2:8888"


// 腾讯cos信息
typedef struct _tTencentInfo{
    XJSON_FUNCI
    // 腾讯云COS:秘钥ID 例如：AKIDutxyynOp1ENI5qY8Xl8KWcmt6JafdUBi
    string secretId;
    // 腾讯云COS:秘钥KEY 例如：b3LSabS7lJxVr495qPlz53sYDqJaeXGz
    string secretKey;
    // 腾讯云COS:储存桶地区 例如：例如：广州(ap-guangzhou),重庆(ap-chongqing)https://cloud.tencent.com/document/product/436/6224
    string region;
    // 腾讯云COS:储存桶名称 例如：例如:jxs-1319980259，https://console.cloud.tencent.com/cos/bucket
    string bucket;
    // 腾讯云COS:上传后储存位置 例如：rv1109/file/
    string remoteFileDir;

    _tTencentInfo(){

    }

    bool operator!=(const _tTencentInfo & p){
        return
                secretId != p.secretId
                || secretKey != p.secretKey
                || region != p.region
                || bucket != p.bucket
                || remoteFileDir != p.remoteFileDir
                ;
    }

    bool operator=(const _tTencentInfo & p){

        secretId = p.secretId;
        secretKey = p.secretKey;
        region = p.region;
        bucket = p.bucket;
        remoteFileDir = p.remoteFileDir;
        return true;
    }

    XPACK( O(secretId, secretKey, region, bucket, remoteFileDir));
}T_TencentInfo;

// aliyun cos信息
typedef struct _tALiYunOssTencentInfo{
    XJSON_FUNCI
    // 阿里云:OSS平台授权key
    string accesskeyId;
    // 阿里云:OSS平台授权accessToken
    string accessToken;
    // 阿里云:OSS端点 例如:http://oss-cn-shenzhen.aliyuncs.com
    string endpoint;
    // 阿里云：OSS名称 例如:qg-device-bucket
    string bucketName;
    // 文件存储位置 例如:rv1109/file/
    string remoteFileDir;

    _tALiYunOssTencentInfo(){

    }

    bool operator!=(const _tALiYunOssTencentInfo & p){
        return
                accesskeyId != p.accesskeyId
                || accessToken != p.accessToken
                || endpoint != p.endpoint
                || bucketName != p.bucketName
                || remoteFileDir != p.remoteFileDir
                ;
    }

    bool operator=(const _tALiYunOssTencentInfo & p){

        accesskeyId = p.accesskeyId;
        accessToken = p.accessToken;
        endpoint = p.endpoint;
        bucketName = p.bucketName;
        remoteFileDir = p.remoteFileDir;
        return true;
    }

    XPACK( O(accesskeyId, accessToken, endpoint, bucketName, remoteFileDir));
}T_ALiYunOssTencentInfo;

// 连接配置
typedef struct _tNetConfig{
    XJSON_FUNCI
    // 连接配置保存位置
    #define NET_CONFIG_FILE "NetConfig.josn"

    // 业务端http请求地址
    string httpUrl;
    // 业务端http鉴权信息
    string httpClientKey;
    // 业务端http请求地址
    string httpClientSecret;
    // 业务端文件服务器地址
    string fileServerUrl;
    // 业务端文件服务器鉴权信息
    string fileServerKey;
    // 业务端文件服务器鉴权信息
    string fileServerSecret;
    // 业务端mqtt地址
    string mqttUrl;
    // 业务端mqtt鉴权信息
    string mqttKey;
    // 业务端mqtt鉴权信息
    string mqttSecret;
    // 设备端http端口地址
    long httpServicePort;
    // 业务端http鉴权信息
    string httpServiceKey;
    // 业务端http鉴权信息
    string httpServiceSecret;
    // 文件上传方式 0：设置的文件服务器地址，1：腾讯云， 2：阿里云
    int fileUploadType;
    // 腾讯连接配置
    T_TencentInfo tencent;
    // 阿里云连接配置
    T_ALiYunOssTencentInfo aliyun;

    // 本地使用的http头
    string localHttpHead;
    _tNetConfig(){
        defaultInit();
    }
    _tNetConfig(string filePath){
        defaultInit();
        this->loadFlie(filePath);
    }

    void defaultInit(){
        // 业务端http请求地址
        httpUrl = "";
        // 业务端http鉴权信息
        httpClientKey = "";
        // 业务端http鉴权信息
        httpClientSecret = "";
        // 业务端文件服务器地址
        fileServerUrl = "";
        // 业务端文件服务器鉴权信息
        fileServerKey = "";
        // 业务端文件服务器鉴权信息
        fileServerSecret = "";
        // 业务端mqtt地址
        mqttUrl = "";
        localHttpHead = DEFALUT_URL_HOST;
        // 业务端mqtt鉴权信息
        mqttKey = "jxs_door_device";
        // 业务端mqtt鉴权信息
        mqttSecret = "jxs_password%ZL2023";
        // 设备端http端口地址
        httpServicePort = 8222;
        // 业务端http鉴权信息
        httpServiceKey = "admin";
        // 业务端http鉴权信息
        httpServiceSecret = "admin";
        fileUploadType = 0;
        this->loadFlie(string("./etc/") + NET_CONFIG_FILE);
    }

    bool operator!=(const _tNetConfig &p){
        return httpUrl != p.httpUrl ||
            httpClientKey != p.httpClientKey ||
            httpClientSecret != p.httpClientSecret ||
            fileServerUrl != p.fileServerUrl ||
            fileServerKey != p.fileServerKey ||
            fileServerSecret != p.fileServerSecret ||
            mqttUrl != p.mqttUrl ||
            mqttKey != p.mqttKey ||
            mqttSecret != p.mqttSecret ||
            httpServicePort != p.httpServicePort ||
            httpServiceKey != p.httpServiceKey ||
            httpServiceSecret != p.httpServiceSecret ||
            fileUploadType != p.fileUploadType ||
            tencent != p.tencent ||
            aliyun != p.aliyun
            ;
    }
    bool operator=(const _tNetConfig &p){
            httpUrl = p.httpUrl;
            httpClientKey = p.httpClientKey;
            httpClientSecret = p.httpClientSecret;
            fileServerUrl = p.fileServerUrl;
            fileServerKey = p.fileServerKey;
            fileServerSecret = p.fileServerSecret;
            mqttUrl = p.mqttUrl;
            mqttKey = p.mqttKey;
            mqttSecret = p.mqttSecret;
            httpServicePort = p.httpServicePort;
            httpServiceKey = p.httpServiceKey;
            httpServiceSecret = p.httpServiceSecret;
            fileUploadType = p.fileUploadType;
            tencent = p.tencent;
            aliyun = p.aliyun;
        return true;
    }


    XPACK( O(httpUrl, httpClientKey, httpClientSecret, fileServerUrl, fileServerKey, fileServerSecret, mqttUrl,
            mqttKey, mqttSecret, httpServicePort, httpServiceKey, httpServiceSecret, localHttpHead, fileUploadType, tencent, aliyun));

}T_NetConfig;

class QTNETWORK_EXPORT CNetConfig:public CNetBase
{
    Q_OBJECT
private:
    // 缓存网络请求时的连接配置
    T_NetConfig m_netConfig;
    T_NetConfig m_devConfig;
    // 单例运行
    static CNetConfig m_Instance;

public:
	CNetConfig();
	// 10分钟超时
	void timerOut();
	static CNetConfig &getInstance(){
		return m_Instance;
	}

public:

public:

	// 获取设备运行模式(主动发起)
	bool sGetConfig();

	// 获取连接配置(主动发起) - 设备端不支持

	// 接收到获取连接配置命令(被动触发)  由 getConfig 命令触发
	bool rGetConfig(struct tRouteData<string> *recv,struct tRouteData<T_NetConfig &> *send, void *userData);

	// 接收到下发连接配置命令(被动触发) 由 downConfig 命令触发
	bool rDownConfig(struct tRouteData<T_NetConfig &> *recv,struct tRouteData<string> *send, void *userData);

	// 获取连接配置
	static T_NetConfig& getConfig(){ return getInstance().__getConfig(); }

	// 保存连接配置
	static void saveConfig(){ getInstance().__saveConfig(); }

	// 获取连接配置
	T_NetConfig& __getConfig(){ return m_devConfig; }

	// 保存连接配置
	void __saveConfig();

	// 恢复出厂设置



};

NETWORKSERVICE_END_NAMESPACE
#endif
